package com.gzxw.mogublog.xo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.File;
import com.gzxw.mogublog.commons.entity.Picture;
import com.gzxw.mogublog.commons.entity.PictureSort;
import com.gzxw.mogublog.commons.feign.PictureFeignClient;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.PictureMapper;
import com.gzxw.mogublog.xo.mapper.PictureSortMapper;
import com.gzxw.mogublog.xo.service.PictureService;
import com.gzxw.mogublog.xo.service.PictureSortService;
import com.gzxw.mogublog.xo.vo.PictureSortVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PictureSortServiceImpl extends ServiceImpl<PictureSortMapper, PictureSort> implements PictureSortService {

    @Autowired
    private PictureFeignClient pictureFeignClient;

    @Override
    public IPage<PictureSort> getPageList(PictureSortVO pictureSortVO) {

        QueryWrapper<PictureSort> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(pictureSortVO.getKeyword())) {
            queryWrapper.like("name", pictureSortVO.getKeyword());
        }

        queryWrapper.orderByDesc("sort", "create_time");

        Page<PictureSort> page = new Page<>();
        page.setCurrent(pictureSortVO.getCurrentPage());
        page.setSize(pictureSortVO.getPageSize());

        IPage<PictureSort> pageList = this.page(page, queryWrapper);
        List<PictureSort> records = pageList.getRecords();

        List<String> fileUids = new ArrayList<>();
        records.forEach(item -> {
            fileUids.add(item.getFileUid());
        });

        // 设置改图片分类对应的图片
        R picResult = null;
        if (fileUids.size()>0) {
            picResult = pictureFeignClient.getPictures(fileUids);
        }
        String data = picResult.getMessage();
        List<File> fileList = JSON.parseArray(data, File.class);

        Map<String, String> pictureSortMap = new HashMap<>();

        fileList.forEach(item -> {
            pictureSortMap.put(item.getUid(), item.getPicUrl());
        });

        records.forEach(item -> {
            item.setPhotoUrl(pictureSortMap.get(item.getFileUid()));
        });

        pageList.setRecords(records);

        return pageList;
    }

    @Override
    public String stickPictureSort(PictureSortVO pictureSortVO) {
        // 根据uid查询出当前图片分类
        PictureSort pictureSort = this.getById(pictureSortVO.getUid());
        // 查找出最大的那个
        QueryWrapper<PictureSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        // 分页查询只查一个
        Page<PictureSort> page = new Page<>();
        page.setCurrent(0);
        page.setSize(1);
        IPage<PictureSort> pageList = this.page(page, queryWrapper);
        List<PictureSort> records = pageList.getRecords();
        PictureSort maxSort = records.get(0);
        if (StringUtils.isEmpty(maxSort.getUid())) {
            return "输入的参数有误!";
        }
        if (maxSort.getUid().equals(pictureSort.getUid())) {
            return "该分类已在顶端";
        }

        Integer count = maxSort.getSort() + 1;
        pictureSort.setSort(count);
        pictureSort.setUpdateTime(new Date());
        pictureSort.updateById();
        return "操作成功";
    }

    @Override
    public String editPictureSort(PictureSortVO pictureSortVO) {

        QueryWrapper<PictureSort> pictureSortQueryWrapper = new QueryWrapper<>();
        pictureSortQueryWrapper.eq("status",1);
        pictureSortQueryWrapper.eq("name",pictureSortVO.getName());
        List<PictureSort> existSorts = this.list(pictureSortQueryWrapper);

        // 查看图片分类名是否存在
        PictureSort pictureSort = this.getById(pictureSortVO.getUid());
        if (existSorts.size()>1||!existSorts.get(0).getUid().equals(pictureSort.getUid())) {
            return "该分类名已经存在";
        }
        pictureSort.setName(pictureSort.getName());
        pictureSort.setParentUid(pictureSortVO.getParentUid());
        pictureSort.setSort(pictureSortVO.getSort());
        pictureSort.setFileUid(pictureSortVO.getFileUid());
        pictureSort.setIsShow(pictureSortVO.getIsShow());
        pictureSort.setUpdateTime(new Date());
        pictureSort.updateById();
        return "编辑成功";
    }
}
